const db = require('../models')

class SessionRepositories {

  async addNewSession(SeccionData) {
    try {
      const newSession = await db.Sessions.create(SeccionData)
      return newSession
    } catch (error) {
      throw error
    }
  }

  async delSessionByUserId(userid) {
    const session = await db.Sessions.findOne({where: { userid }})
    return session ? (session.destroy(), true) : false
  }

  async delAllSessions() {
    const deletedCount = await db.Sessions.destroy({
      where: {} //空条件就是删除所有数据
    })
    return deletedCount
  }
  
  async getSessionByUserId(userid) {
    return await db.Sessions.findOne({ where: { userid } })
  }
}

module.exports = new SessionRepositories